package demo4;

public class Sort {
    //插入排序
    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > temp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = temp;
        }
    }

    //希尔排序
    public void shellSort(int[] array) {
        int group = array.length / 2;
        while (group > 0) {
            insertSort1(array,group,array.length - 1);
            group /= 2;
        }
    }
    public void insertSort1(int[] array, int start, int end) {
        for (int i = start; i <= end; i++) {
            int temp = array[i];
            int j = i - start;
            for (; j >= 0; j -= start) {
                if (array[j] > temp) {
                    array[j + start] = array[j];
                } else {
                    break;
                }
            }
            array[j + start] = temp;
        }
    }
    //选择排序
    public void selectSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    // 堆排序
    public void heapSort(int[] array) {
        createHeap(array);
        int usedSize = array.length - 1;
        for (int i = 0; i < array.length; i++) {
            swap(array,0,usedSize--);
            shiftDown(array,0,usedSize - 1);
        }
    }
    //建立大根堆
    public void createHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(array,parent,array.length - 1);
        }
    }

    /**
     *
     * @param root 是每棵子树的根节点的下标
     * @param len  是每棵子树调整结束的结束条件
     * 向下调整的时间复杂度：O(logn)
     */
    private void shiftDown(int[] array,int root,int len) {
        int child = root * 2 + 1;
        while (child <= len) {
            if ((child + 1 <= len) && (array[child + 1] > array[child])) {
                child++;
            }
            if (array[root] < array[child]) {
                swap(array,root,child);
                root = child;
                child = root * 2 + 1;
            } else {
                break;
            }
        }
    }
    //冒泡排序
    public void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    flag = false;
                    swap(array,j,j + 1);
                }
            }
            if (flag) {
                break;
            }
        }
    }

    //快速排序
    public void quickSort(int[] array) {
        quick(array,0,array.length - 1);
    }
    public void quick(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = partition(array,left,right);
        quick(array,left,mid - 1);
        quick(array,mid + 1,right);
    }

    public int partition(int[] array, int left, int right) {
        int temp = array[left];
        while (left < right) {
            if (left < right && array[right] >= temp) {
                right--;
            }
            array[left] = array[right];
            if (left < right && array[left] <= temp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = temp;
        return left;
    }


    //交换
    public void swap(int[] array, int x, int y) {
        int temp = array[x];
        array[x] = array[y];
        array[y] = temp;
    }
}
